package com.zhenta.controller;

import com.zhentao.pojo.PatientInfo;
import com.zhentao.pojo.PatientInfoVO;
import com.zhentao.pojo.Result;
import com.zhentao.pojo.RelationType;
import com.zhentao.pojo.IdCardType;
import com.zhentao.service.PatientInfoService;
import com.zhentao.service.RelationTypeService;
import com.zhentao.service.IdCardTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 就诊人控制器
 */
@RestController
@RequestMapping("/patient")
@CrossOrigin(origins = "*")
public class PatientController {

    @Autowired
    private PatientInfoService patientInfoService;
    
    @Autowired
    private RelationTypeService relationTypeService;
    
    @Autowired
    private IdCardTypeService idCardTypeService;

    /**
     * 获取所有就诊人信息列表
     */
    @GetMapping("/list")
    public Result<List<PatientInfoVO>> getPatientList() {
        try {
            List<PatientInfoVO> patientList = patientInfoService.getPatientInfoList();
            return Result.success(patientList);
        } catch (Exception e) {
            return Result.error("获取就诊人列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取就诊人信息
     */
    @GetMapping("/{id}")
    public Result<PatientInfoVO> getPatientById(@PathVariable Integer id) {
        try {
            PatientInfoVO patientInfo = patientInfoService.getPatientInfoById(id);
            if (patientInfo != null) {
                return Result.success(patientInfo);
            } else {
                return Result.error("就诊人信息不存在");
            }
        } catch (Exception e) {
            return Result.error("获取就诊人信息失败：" + e.getMessage());
        }
    }

    /**
     * 添加就诊人信息
     */
    @PostMapping("/add")
    public Result<String> addPatient(@RequestBody PatientInfo patientInfo) {
        try {
            // 数据验证
            if (patientInfo.getName() == null || patientInfo.getName().trim().isEmpty()) {
                return Result.error("就诊人姓名不能为空");
            }
            if (patientInfo.getRelationTypeId() == null) {
                return Result.error("关系类型不能为空");
            }
            if (patientInfo.getIdCardTypeId() == null) {
                return Result.error("证件类型不能为空");
            }
            if (patientInfo.getIdCardNumber() == null || patientInfo.getIdCardNumber().trim().isEmpty()) {
                return Result.error("证件号码不能为空");
            }
            if (patientInfo.getPhoneNumber() == null || patientInfo.getPhoneNumber().trim().isEmpty()) {
                return Result.error("手机号码不能为空");
            }

            // 设置创建时间和更新时间
            patientInfo.setCreateTime(new Date());
            patientInfo.setUpdateTime(new Date());
            
            // 如果设置为默认就诊人，需要将其他就诊人的默认状态取消
            if (patientInfo.getIsDefault() != null && patientInfo.getIsDefault() == 1) {
                patientInfoService.clearDefaultPatient();
            } else {
                patientInfo.setIsDefault(0);
            }

            boolean success = patientInfoService.save(patientInfo);
            if (success) {
                return Result.success("添加就诊人成功");
            } else {
                return Result.error("添加就诊人失败");
            }
        } catch (Exception e) {
            return Result.error("添加就诊人失败：" + e.getMessage());
        }
    }

    /**
     * 更新就诊人信息
     */
    @PutMapping("/update")
    public Result<String> updatePatient(@RequestBody PatientInfo patientInfo) {
        try {
            if (patientInfo.getId() == null) {
                return Result.error("就诊人ID不能为空");
            }

            // 设置更新时间
            patientInfo.setUpdateTime(new Date());
            
            // 如果设置为默认就诊人，需要将其他就诊人的默认状态取消
            if (patientInfo.getIsDefault() != null && patientInfo.getIsDefault() == 1) {
                patientInfoService.clearDefaultPatient();
            }

            boolean success = patientInfoService.updateById(patientInfo);
            if (success) {
                return Result.success("更新就诊人信息成功");
            } else {
                return Result.error("更新就诊人信息失败");
            }
        } catch (Exception e) {
            return Result.error("更新就诊人信息失败：" + e.getMessage());
        }
    }

    /**
     * 删除就诊人信息
     */
    @DeleteMapping("/{id}")
    public Result<String> deletePatient(@PathVariable Integer id) {
        try {
            boolean success = patientInfoService.deletePatient(id);
            if (success) {
                return Result.success("删除就诊人成功");
            } else {
                return Result.error("删除就诊人失败");
            }
        } catch (Exception e) {
            return Result.error("删除就诊人失败：" + e.getMessage());
        }
    }

    /**
     * 设置默认就诊人
     */
    @PutMapping("/setDefault/{id}")
    public Result<String> setDefaultPatient(@PathVariable Integer id) {
        try {
            boolean success = patientInfoService.setDefaultPatient(id);
            if (success) {
                return Result.success("设置默认就诊人成功");
            } else {
                return Result.error("设置默认就诊人失败");
            }
        } catch (Exception e) {
            return Result.error("设置默认就诊人失败：" + e.getMessage());
        }
    }

    /**
     * 获取关系类型列表
     */
    @GetMapping("/relationTypes")
    public Result<List<RelationType>> getRelationTypes() {
        try {
            List<RelationType> relationTypes = relationTypeService.list();
            return Result.success(relationTypes);
        } catch (Exception e) {
            return Result.error("获取关系类型列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取证件类型列表
     */
    @GetMapping("/idCardTypes")
    public Result<List<IdCardType>> getIdCardTypes() {
        try {
            List<IdCardType> idCardTypes = idCardTypeService.list();
            return Result.success(idCardTypes);
        } catch (Exception e) {
            return Result.error("获取证件类型列表失败：" + e.getMessage());
        }
    }
} 